package com.brillilab.starter.controller.ios.lab;

import com.brillilab.service.logic.lab.LabMemberLogic;
import com.brillilab.starter.handler.RequestPower;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.pa.lab.PowerModifyPa;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.lab.ILabMemberPowerService;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.entity.req.lab.PowerBatchModifyReqList;
import com.brillilab.starter.entity.req.lab.PowerModifyReq;

import javax.annotation.Resource;
import java.util.List;


/**
 * 实验室成员权限API
 *
 * @author lj
 */
@RestController("IosLabMemberPowerController")
@RequestMapping("/ios/labMemberPower")
public class LabMemberPowerController {

	@Resource
	private ILabMemberPowerService labMemberPowerService;
	@Resource
	private ILabMemberService labMemberService;
	@Resource
	private LabMemberLogic labMemberLogic;
	@Resource
	private UsersLogic usersLogic;


    /**
     * 修改实验室成员权限
     *
     * @param req
     * @return
     */
	@AspectLog
    @RequestMapping(value="/modify",method=RequestMethod.POST)
	@RequestPower(LabMemberPowerEnum.user_manage)
    public ResponseVo modifyPower(@RequestBody PowerModifyReq req,@RequestHeader(name=StarterConstant.TOKEN) String token) {


		Long labMemberId = req.getLabMemberId();
		String[] powers = req.getPowers();

		Assert.notNull(labMemberId, "请求参数缺失！");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);

		labMemberLogic.doModifyPower(labMemberId,powers,userInfo);
		return ResponseVo.success();
	}

    /**
     * 实验室成员权限开关
     *
     * @param req
     * @param token
     * @return
     */
	@AspectLog
    @RequestMapping(value="/modifyOne",method=RequestMethod.POST)
	@RequestPower(LabMemberPowerEnum.user_manage)
    public ResponseVo modifyPowerOne(@RequestBody PowerModifyPa req,@RequestHeader(name=StarterConstant.TOKEN) String token){

        Long labMemberId=req.getLabMemberId();
        String power=req.getPower();
        Integer isOpen=req.getIsOpen();
        Assert.isTrue(labMemberId!=null && !StringUtils.isBlank(power) && isOpen!=null,"请求参数缺失!");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

		labMemberLogic.doModifyPowerOne(labMemberId,power,isOpen,userInfo);

		return ResponseVo.success();
    }

    /**
     * 批量更新实验室成员权限
     *
     * @return
     */
	@AspectLog
    @PostMapping("/batchModify")
	@RequestPower(LabMemberPowerEnum.user_manage)
    public ResponseVo batchModifyPower(@RequestBody PowerBatchModifyReqList pbmrList,@RequestHeader(name=StarterConstant.TOKEN) String token) {
		List<PowerModifyPa> reqList=pbmrList.getReqList();
		UserInfoVo userInfo=usersLogic.getUserByToken(token);
		labMemberLogic.doBatchModifyPower(reqList,userInfo);
		return ResponseVo.success();
    }


}
